package com.nutrition.health.repository;

import com.nutrition.health.model.DietRecord;
import com.nutrition.health.model.DietRecord.MealType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface DietRecordRepository extends JpaRepository<DietRecord, Long> {

    List<DietRecord> findByUserId(Long userId);

    Page<DietRecord> findByUserId(Long userId, Pageable pageable);

    List<DietRecord> findByUserIdAndMealType(Long userId, MealType mealType);
    
    List<DietRecord> findByUserIdAndMealTypeAndConsumptionTimeBetween(
            Long userId,
            MealType mealType,
            LocalDateTime startTime,
            LocalDateTime endTime);

    List<DietRecord> findByUserIdAndConsumptionTimeBetween(
            Long userId,
            LocalDateTime startTime,
            LocalDateTime endTime);

    @Query("SELECT dr FROM DietRecord dr JOIN dr.foodItems fi " +
           "WHERE dr.userId = :userId AND fi.id = :foodItemId")
    List<DietRecord> findByUserIdAndFoodItemId(
            @Param("userId") Long userId,
            @Param("foodItemId") Long foodItemId);

    @Query("SELECT SUM(ni.calories) FROM DietRecord dr " +
           "JOIN dr.foodItems fi " +
           "JOIN NutritionInfo ni ON fi.id = ni.foodItemId " +
           "WHERE dr.userId = :userId AND " +
           "dr.consumptionTime BETWEEN :startTime AND :endTime")
    Double calculateTotalCaloriesForUserInTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    @Query("SELECT SUM(ni.protein) FROM DietRecord dr " +
           "JOIN dr.foodItems fi " +
           "JOIN NutritionInfo ni ON fi.id = ni.foodItemId " +
           "WHERE dr.userId = :userId AND " +
           "dr.consumptionTime BETWEEN :startTime AND :endTime")
    Double calculateTotalProteinForUserInTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
            
    @Query("SELECT SUM(ni.carbohydrates) FROM DietRecord dr " +
           "JOIN dr.foodItems fi " +
           "JOIN NutritionInfo ni ON fi.id = ni.foodItemId " +
           "WHERE dr.userId = :userId AND " +
           "dr.consumptionTime BETWEEN :startTime AND :endTime")
    Double calculateTotalCarbsForUserInTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
            
    @Query("SELECT SUM(ni.fat) FROM DietRecord dr " +
           "JOIN dr.foodItems fi " +
           "JOIN NutritionInfo ni ON fi.id = ni.foodItemId " +
           "WHERE dr.userId = :userId AND " +
           "dr.consumptionTime BETWEEN :startTime AND :endTime")
    Double calculateTotalFatForUserInTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
            
    @Query("SELECT SUM(dr.calories) FROM DietRecord dr " +
           "WHERE dr.userId = :userId AND " +
           "dr.consumptionTime BETWEEN :startTime AND :endTime AND " +
           "dr.mealType = :mealType")
    Double calculateTotalCaloriesByMealType(
            @Param("userId") Long userId,
            @Param("mealType") MealType mealType,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
}